/**

 @Name：layuiAdmin 用户管理 管理员管理 角色管理
 @Author：star1029
 @Site：http://www.layui.com/admin/
 @License：LPPL

 */


layui.define(['table', 'form'], function(exports){
    var $ = layui.$
    ,table = layui.table
    ,form = layui.form;

    //用户管理
    table.render({
        elem: '#LAY-user-manage'
        ,url: "/user"
        ,cols: [[
            {type: 'checkbox', fixed: 'left'}
            ,{field: 'id', width: 100, title: 'ID', sort: true}
            ,{field: 'username', title: '用户名', minWidth: 100}
            ,{field: 'avatar', title: '头像', width: 100, templet: '#imgTpl'}
            ,{field: 'phone', title: '手机'}
            ,{field: 'email', title: '邮箱'}
            ,{field: 'gender', width: 80, title: '性别'}
            ,{field: 'created_at', title: '创建时间'}
            ,{field: 'updated_at', title: '更新时间'}
            ,{title: '操作', width: 150, align:'center', fixed: 'right', toolbar: '#table-useradmin-webuser'}
        ]]
        ,page: true
        ,limit: 30
        ,height: 'full-220'
        ,text: '对不起，加载出现异常！'
    });

    //监听工具条
    table.on('tool(LAY-user-manage)', function(obj){
        var data = obj.data;
        if(obj.event === 'del'){
            layer.prompt({
            formType: 1
            ,title: '敏感操作，请验证口令'
            }, function(value, index){
                layer.close(index);
                layer.confirm('真的删除行么', function(index){
                    $.post("/admin/user/destroy",{_method:'delete',ids:[data.id]},function (result) {
                        if (result.code===0){
                            obj.del(); //删除对应行（tr）的DOM结构
                        }
                        layer.close(index);
                        layer.msg(result.msg,{icon:6})
                    });
                });

            });
        } else if(obj.event === 'edit'){
            var tr = $(obj.tr);

            layer.open({
                type: 2
                ,title: '编辑用户'
                ,content: '/user/'+data.id+'/edit'
                ,maxmin: true
                ,area: ['500px', '450px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-user-front-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);

                    //监听提交
                    iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                        var field = data.field; //获取提交的字段

                        //提交 Ajax 成功后，静态更新表格中的数据
                        //$.ajax({});
                        table.reload('LAY-user-manage'); //数据刷新
                        layer.close(index); //关闭弹层
                    });
                    submit.trigger('click');
                }
                ,success: function(layero, index){

                }
            });
        }
    });

    //管理员管理
    table.render({
        elem: '#LAY-user-back-manage'
        ,url: 'admin'
        ,page: true //开启分页
        ,cols: [[
            {type: 'checkbox', fixed: 'left'}
            ,{field: 'id', width: 80, title: 'ID', sort: true}
            ,{field: 'name', title: '用户名'}
            ,{field: 'email', title: '邮箱'}
            ,{field: 'phone', title: '电话'}
            ,{field: 'created_at', title: '创建时间'}
            ,{field: 'updated_at', title: '更新时间'}
            ,{title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-useradmin-admin'}
        ]]
        ,text: '对不起，加载出现异常！'
    });

    //监听工具条
    table.on('tool(LAY-user-back-manage)', function(obj) {
        var data = obj.data;
        if(obj.event === 'del') {
            layer.prompt({
                formType: 1
                ,title: '敏感操作，请验证口令'
            }, function(value, index){
                layer.close(index);
                layer.confirm('确定删除此管理员？', function(index){
                    $.post("/admin/user/destroy", {_method:'delete',ids:[data.id]},function (result) {
                        if (result.code===0){
                            obj.del(); //删除对应行（tr）的DOM结构
                        }
                        layer.close(index);
                        layer.msg(result.msg,{icon:6})
                    });
                });
             });
        }else if(obj.event === 'edit') {
            var tr = $(obj.tr);

            layer.open({
                type: 2
                ,title: '编辑管理员'
                ,content: '/admin/admin/'+data.id+'/edit'
                ,area: ['420px', '420px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero) {
                    var iframeWindow = window['layui-layer-iframe'+ index]
                        ,submitID = 'LAY-user-back-submit'
                        ,submit = layero.find('iframe').contents().find('#'+ submitID);

                    //监听提交
                    iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                        var field = data.field; //获取提交的字段

                        //提交 Ajax 成功后，静态更新表格中的数据
                        $.ajax({
                            url:data.form.action,
                            data:field,
                            type:"put",
                            dataType:"json",
                            success:function(data){
                                if(data.code===0){
                                    layer.msg(data.msg, {icon: 6});
                                    table.reload('LAY-user-front-submit'); //数据刷新
                                    layer.close(index); //关闭弹层
                                }else{
                                    layer.msg(data.msg, {icon: 5});
                                }
                            },
                            error:function(data){
                                layer.msg(data.msg);
                            }
                        });
                    });

                    submit.trigger('click');
                }
                ,success: function(layero, index){

                }
            })
        } else if (obj.event === 'role') {
            layer.open({
                type: 2
                ,title: '分配角色'
                ,content: '/admin/admin/'+data.id+'/role'
                ,area: ['420px', '420px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    const iframeWindow = window['layui-layer-iframe' + index]
                        , submitID = 'submit'
                        , submit = layero.find('iframe').contents().find('#' + submitID);

                    //监听提交
                    iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                        const field = data.field; //获取提交的字段
                        //提交 Ajax 成功后，静态更新表格中的数据
                        $.ajax({
                            url:data.form.action,
                            data:field,
                            type:"put",
                            dataType:"json",
                            success:function(data){
                                if(data.code===0){
                                    layer.msg(data.msg, {icon: 6});
                                    table.reload('dataTable'); //数据刷新
                                    layer.close(index); //关闭弹层
                                }else{
                                    layer.msg(data.msg, {icon: 5});
                                }
                            },
                            error:function(data){
                                layer.msg(data.msg);
                            }
                        });
                    });

                    submit.trigger('click');
                }
            })
        } else if (obj.event === 'permission'){
            layer.open({
                type: 2
                ,title: '分配权限'
                ,content: '/admin/admin/'+data.id+'/permission'
                ,area: ['500px', '420px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    const iframeWindow = window['layui-layer-iframe' + index]
                        , submitID = 'submit'
                        , submit = layero.find('iframe').contents().find('#' + submitID);

                    //监听提交
                    iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                        const field = data.field; //获取提交的字段
                        //提交 Ajax 成功后，静态更新表格中的数据
                        $.ajax({
                            url:data.form.action,
                            data:field,
                            type:"put",
                            dataType:"json",
                            success:function(res){
                                if(res.code===0){
                                    layer.msg(res.msg, {icon: 6});
                                    layer.close(index); //关闭弹层
                                }else{
                                    layer.msg(res.msg, {icon: 5});
                                }
                            },
                            error:function(res){
                                layer.msg(res.msg);
                            }
                        });
                    });

                    submit.trigger('click');
                }
            })
        }
    });

    //角色管理
    table.render({
        elem: '#LAY-user-back-role'
        ,url: '/role'
        ,where:{model:"role"}
        ,page: true
        ,cols: [[
            {type: 'checkbox', fixed: 'left'}
            ,{field: 'id', title: 'ID', sort: true,width:80}
            ,{field: 'name', title: '名称'}
            ,{field: 'display_name', title: '显示名称'}
            ,{field: 'created_at', title: '创建时间'}
            ,{field: 'updated_at', title: '更新时间'}
            ,{title: '操作',width: 200, align: 'center', fixed: 'right', toolbar: '#table-useradmin-admin'}
        ]]
        ,text: '对不起，加载出现异常！'
    });

    //监听工具条
    table.on('tool(LAY-user-back-role)', function(obj){
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定删除此角色？', function(index){
            $.post("/role/destroy",{_method:'delete',ids:[data.id]},function (result) {
                if (result.code===0){
                    obj.del(); //删除对应行（tr）的DOM结构
                }
                layer.close(index);
                layer.msg(result.msg,{icon:6})
                });
            });
        } else if(obj.event === 'edit') {
            var tr = $(obj.tr);
            layer.open({
                type: 2
                ,title: '编辑角色'
                ,content: '/admin/role/'+data.id+'/edit'
                ,area: ['500px', '480px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    var iframeWindow = window['layui-layer-iframe'+ index]
                    ,submit = layero.find('iframe').contents().find("#LAY-user-role-submit");

                    //监听提交
                    iframeWindow.layui.form.on('submit(LAY-user-role-submit)', function(data){
                        var field = data.field; //获取提交的字段

                        //提交 Ajax 成功后，静态更新表格中的数据
                        //$.ajax({});
                        table.reload('LAY-user-back-role'); //数据刷新
                        layer.close(index); //关闭弹层
                    });
                    submit.trigger('click');
                }
                ,success: function(layero, index){

                }
            })
        } else if (obj.event === 'permission') {
            layer.open({
                type: 2
                ,title: '分配权限'
                ,content: '/role/'+data.id+'/permission'
                ,area: ['500px', '420px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    const iframeWindow = window['layui-layer-iframe' + index]
                        , submitID = 'submit'
                        , submit = layero.find('iframe').contents().find('#' + submitID);

                    //监听提交
                    iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                        const field = data.field; //获取提交的字段
                        //提交 Ajax 成功后，静态更新表格中的数据
                        $.ajax({
                            url:data.form.action,
                            data:field,
                            type:"put",
                            dataType:"json",
                            success:function(res){
                                if(res.code===0){
                                    layer.msg(res.msg, {icon: 6});
                                    layer.close(index); //关闭弹层
                                }else{
                                    layer.msg(res.msg, {icon: 5});
                                }
                            },
                            error:function(res){
                                layer.msg(res.msg);
                            }
                        });
                    });

                    submit.trigger('click');
                }
            })
        }
    });

    //权限管理
    table.render({
        elem: '#LAY-user-back-permission'
        ,url: "permission" //数据接口
        ,page: true //开启分页
        ,cols: [[ //表头
            {checkbox: true,fixed: true}
            ,{field: 'id', title: 'ID', sort: true,width:80}
            ,{field: 'name', title: '权限名称'}
            ,{field: 'title', title: '显示名称'}
            ,{field: 'route', title: '路由'}
            ,{field: 'icon', title: '图标',width:60, toolbar:'#icon'}
            ,{field: 'created_at', title: '创建时间'}
            ,{field: 'updated_at', title: '更新时间'}
            ,{fixed: 'right', width: 260, align:'center', toolbar: '#table-useradmin-admin'}
        ]]
    });

    //监听工具条
    table.on('tool(LAY-user-back-permission)', function(obj){ //注：tool是工具条事件名，dataTable是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data //获得当前行数据
            ,layEvent = obj.event; //获得 lay-event 对应的值
        if(layEvent === 'del'){
            layer.confirm('确认删除吗？', function(index){
                $.post("permission/destroy",{_method:'delete',ids:[data.id]},function (result) {
                    if (result.code===0){
                        obj.del(); //删除对应行（tr）的DOM结构
                    }
                    layer.close(index);
                    layer.msg(result.msg,{icon:6})
                });
            });
        } else if(layEvent === 'edit'){
            layer.open({
                type: 2
                ,title: '编辑权限'
                ,content: '/admin/permission/'+data.id+'/edit'
                ,area: ['100%', '100%']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    var iframeWindow = window['layui-layer-iframe'+ index]
                        ,submitID = 'LAY-user-back-submit'
                        ,submit = layero.find('iframe').contents().find('#'+ submitID);

                    //监听提交
                    iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                        var field = data.field; //获取提交的字段

                        //提交 Ajax 成功后，静态更新表格中的数据
                        //$.ajax({});
                        table.reload('LAY-user-front-submit'); //数据刷新
                        layer.close(index); //关闭弹层
                    });

                    submit.trigger('click');
                }
            });
        } else if (layEvent === 'children'){
            var pid = $("#back").attr("data-val");
            if (data.parent_id!==0){
                $("#back").attr("data-val",pid+'_'+data.parent_id);
            }
            //执行重载
            table.reload('LAY-user-back-permission', {
                where:{parent_id:data.id},
                page:{curr:1}
            });
        }
    });

    exports('useradmin', {})
});
